package com.ywd.platform.controller;

import com.ywd.platform.context.UserContext;
import com.ywd.platform.dto.UserDto;
import com.ywd.platform.dto.UserLoginDto;
import com.ywd.platform.page.UserPageDto;
import com.ywd.platform.exception.R;
import com.ywd.platform.exception.RPage;
import com.ywd.platform.service.impl.UserServiceImpl;
import com.ywd.platform.vo.UserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lk
 * @date 2024/4/17 14:02
 */
@AllArgsConstructor
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理")
public class UserController {

    private final UserServiceImpl userService;

    @GetMapping("/getUserInfo")
    @Operation(summary = "查询当前用户信息")
    public R<UserVo> getUserInfo(){
        return R.ok(UserContext.getUser());
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public R<UserVo> login(@RequestBody UserLoginDto dto) {
        return R.ok(userService.login(dto));
    }

    @PutMapping("/resetPassword/{id}")
    @Operation(summary = "重置密码")
    public R resetPassword(@Schema(description = "用户ID") @PathVariable Integer id) {
        return R.ok(userService.resetPassword(id));
    }

    @PostMapping("/saveUser")
    @Operation(summary = "添加用户")
    public R<Boolean> saveUser(@RequestBody UserDto dto){
        return R.ok(userService.saveUser(dto));
    }

    @PutMapping("/updateUser")
    @Operation(summary = "修改用户")
    public R<Boolean> updateUser(@RequestBody UserDto dto){
        return R.ok(userService.updateUser(dto));
    }

    @DeleteMapping("/deleteUser/{id}")
    @Operation(summary = "删除用户")
    public R<Boolean> deleteUser(@Schema(description = "用户ID") @PathVariable("id") Integer id){
        return R.ok(userService.deleteUser(id));
    }

    @PostMapping("/getUserPage")
    @Operation(summary = "查询用户列表")
    public RPage<List<UserVo>> getUserList(@RequestBody UserPageDto dto){
        return userService.getUserList(dto);
    }

    @GetMapping("/getUserDetails/{id}")
    @Operation(summary = "查询用户详情")
    public R<UserVo> getUserDetails(@Schema(description = "用户ID") @PathVariable("id") Integer id){
        return R.ok(userService.getUserDetails(id));
    }

}
